package com.spark.incoicings.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spark.incoicings.dao.InventorysMapper;
import com.spark.incoicings.dao.ProductMapper;
import com.spark.incoicings.service.InventorysService;
import com.spark.invoicings.entity.Inventorys;
import com.spark.invoicings.entity.Product;
import com.spark.invoicings.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class InventorysServiceImpl extends ServiceImpl<InventorysMapper, Inventorys> implements InventorysService {

    @Resource
    private InventorysMapper inventorysMapper;

    @Resource
    private ProductMapper productMapper;

    @Override
    public Inventorys selectInventorysById(Integer id) {
        return inventorysMapper.selectInventorysById(id);
    }

    /**
     * 查询所有盘点单
     * @param map
     * @return
     */
    @Override
    public List<Inventorys> selectInventorysAll(Map<String, Object> map) {
        return inventorysMapper.selectInventorysAll(map);
    }

    /**
     * 添加盘点单
     * @param inventorys
     * @return
     */
    @Transactional
    @Override
    public Result addInventorys(Inventorys inventorys) {
        // 计算盈亏
        inventorys.setProfitLossQuantity(inventorys.getActualQuantity() - inventorys.getBookQuantity());

        Product product = productMapper.selectProductById(inventorys.getProductId());
        int res = 0;

        // 库存增减计算
        if (inventorys.getProfitLossQuantity() > 0) {
            res = product.getCurrentInventory() - inventorys.getProfitLossQuantity();
            product.setCurrentInventory(res);
            productMapper.updateById(product);
        } else {
            res = product.getCurrentInventory() + inventorys.getProfitLossQuantity();
            product.setCurrentInventory(res);
            productMapper.updateById(product);
        }
        inventorysMapper.insert(inventorys);
        return Result.success(200, "success", inventorys.getId());
    }

    /**
     * 根据id更新盘点单
     * @param inventorys
     * @return
     */
    @Override
    public boolean updateInventorysById(Inventorys inventorys) {
        // 计算盈亏
        inventorys.setProfitLossQuantity(inventorys.getActualQuantity() - inventorys.getBookQuantity());

        Product product = productMapper.selectProductById(inventorys.getProductId());
        int res = 0;

        // 库存增减计算
        if (inventorys.getProfitLossQuantity() > 0) {
            res = product.getCurrentInventory() - inventorys.getProfitLossQuantity();
            product.setCurrentInventory(res);
            productMapper.updateById(product);
        } else {
            res = product.getCurrentInventory() + inventorys.getProfitLossQuantity();
            product.setCurrentInventory(res);
            productMapper.updateById(product);
        }
        return inventorysMapper.updateById(inventorys) > 0;
    }

}
